package br.edu.univas.es2012.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

import br.edu.univas.es2012.listeners.ButtonsListener;

public class PanelButtons extends JPanel{
	private static final long serialVersionUID = 1L;
	
	private JButton okButton;
	private JButton cancelButton;
	
	private ArrayList<ButtonsListener> listeners = new ArrayList<ButtonsListener>();
	
	public PanelButtons() {
		super();
		initialize();
	}

	private void initialize() {
		add(getOkButton());
		add(getCancelButton());
		
	}

	private JButton getOkButton() {
		if(okButton == null){
			URL caminho = getClass().getResource("/Imagens/ok.png");
			okButton = new JButton("Ok", new ImageIcon(caminho));
			okButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					for(ButtonsListener listener : listeners){
						listener.gravar();
					}
					
				}
			});
		}
		return okButton;
	}

	private JButton getCancelButton() {
		if(cancelButton == null){
			URL caminho = getClass().getResource("/Imagens/cancel.png");
			cancelButton = new JButton("Cancelar", new ImageIcon(caminho));
			cancelButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					for(ButtonsListener listener : listeners){
						listener.cancelar();
					}
					
				}
			});
		}
		
		return cancelButton;
	}
	
	public void addButtonsListener(ButtonsListener listener){
		if(listener != null){
			listeners.add(listener);
		}
	}
	
	

}
